﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using NetJsWire.Lite.Converters;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    public class FormPanel : Panel
    {
        private Api _api = new Api();

        public FormPanel()
            : this( null )
        {
        }

        public FormPanel( IContainer container )
            : base( container )
        {
            base.Xtype = "form";
        }

        [JsonProperty( "api" )]
        [TypeConverter( typeof( ExpandableObjectConverter ) )]
        public Api Api { get { return _api; } set { _api = value; } }

        [DefaultValue( false )]
        [JsonProperty( "paramsAsHash" )]
        public bool ParamsAsHash { get; set; }

        /// <summary>
        ///   If true, the form monitors its valid state client-side and regularly fires the clientvalidation event passing that state.
        ///   When monitoring valid state, the FormPanel enables/disables any of its configured buttons which have been configured with formBind: true depending on whether the form is valid or not. Defaults to false
        /// </summary>
        [DefaultValue( false )]
        [JsonProperty( "monitorValid" )]
        [Description( "If true, the form monitors its valid state client-side and regularly fires the clientvalidation event passing that state." )]
        public bool MonitorValid { get; set; }

        [JsonProperty( "waitMsgTarget", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [DefaultValue( ( string ) null )]
        public string WaitMsgTarget { get; set; }

        [JsonProperty( "waitMessage", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        public string WaitMessage { get; set; }

        [JsonProperty( "waitTitle", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        public string WaitTitle { get; set; }

        public bool ShouldSerializeApi()
        {
            return !string.IsNullOrWhiteSpace( Api.Load ) || !string.IsNullOrWhiteSpace( Api.Submit );
        }

        protected void ResetApi()
        {
            Api.Load = null;
            Api.Submit = null;
        }
    }
}
